package net.beyondts.mplug.utils;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

public class AspectUtils {
    public static Object findArg(JoinPoint joinPoint, String argName) {
        String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
        Object[] argValues = joinPoint.getArgs();
        for (int i = 0; i < argNames.length; i++) {
            if (StringUtils.equals(argName, argNames[i])) {
                return argValues[i];
            }
        }
        return null;
    }

    public static <T> T findArg(JoinPoint joinPoint, Class<T> clazz) {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (clazz.isInstance(arg)) {
                return clazz.cast(arg);
            }
        }
        return null;
    }
}
